<?php

namespace app\common\model;

use think\Db;
use think\Model;

class FundsRecord extends Model
{
	// 表名
	protected $name = 'funds_record';
	
	// 自动写入时间戳字段
	protected $autoWriteTimestamp = true;
	
	// 定义时间戳字段名
	protected $updateTime = false;
	
	// 追加属性
	protected $append = [
		'create_time_text',
		'type_text',
		'operation_type_text',
		'account_type_text'
	];
	
	const OPE_TYPE_PLUS = 1;
	const OPE_TYPE_LESS = 2;
	
	public static $_operation_type = [
		self::OPE_TYPE_PLUS => '添加',
		self::OPE_TYPE_LESS => '减少'
	];
	
	
	public function getOperationTypeList()
	{
		return self::$_operation_type;
	}
	
	
	const ACCOUNT_TYPE_BALANCE = 1;
	const ACCOUNT_TYPE_INTEGRAL = 2;
	const ACCOUNT_TYPE_PHONE = 3;
	const ACCOUNT_TYPE_VOUCHER = 4;
	const ACCOUNT_TYPE_DEPOSIT = 5;
	const ACCOUNT_TYPE_VIDEO = 6;

	public static $_account_type = [
		self::ACCOUNT_TYPE_BALANCE => '余额',
		self::ACCOUNT_TYPE_INTEGRAL => '积分',
		self::ACCOUNT_TYPE_PHONE => '话费',
		self::ACCOUNT_TYPE_VOUCHER => '优惠券',
		self::ACCOUNT_TYPE_DEPOSIT => '押金',
		self::ACCOUNT_TYPE_VIDEO => '影视'
	];
	
	
	public static $_account_field = [
		self::ACCOUNT_TYPE_BALANCE => 'balance',
		self::ACCOUNT_TYPE_INTEGRAL => 'integral',
		self::ACCOUNT_TYPE_PHONE => 'phone_balance',
		self::ACCOUNT_TYPE_VOUCHER => 'voucher_balance',
		self::ACCOUNT_TYPE_DEPOSIT => 'deposit_balance',
		self::ACCOUNT_TYPE_VIDEO => 'video_time'
	];
	
	public function getAccountTypeList()
	{
		return self::$_account_type;
	}
	
	
	const TYPE_SIGN = 1;
	const TYPE_WITHDRAW = 2;
	const TYPE_PAY_ORDER = 3;
	const TYPE_TRANSFER = 4;
	const TYPE_REBATE = 5;
	const TYPE_CART_REBATE = 6;
	const TYPE_CART_ORDER_OUT = 7;
	const TYPE_VOUCHER_EXCHANGE = 8;
	const TYPE_AFTERSALES_RETURN = 9;
	const TYPE_ORDER_GIVE = 10;
	const TYPE_RECHARGE = 11;
	const TYPE_PAY_RECHARGE = 12;
	const TYPE_CARD_RECHARGE = 13;
	const TYPE_WITHDRAW_RETURN = 14;
	const TYPE_CARTORDER_GIFT = 15;
	const TYPE_VOUCHER_PDD = 16;
	const TYPE_VOUCHER_REBATE = 17;
	const TYPE_CALL = 18;
	const TYPE_GOODS_DEPOSIT = 19;
	const TYPE_FUND_DEPOSIT = 20;
	const TYPE_ORDER_REBACK = 21;
	const TYPE_VOUCHER_JD = 22;
	const TYPE_ADMIN_OPERATE = 23;
	const TYPE_ORDER_REIMBURSE = 24;
	const TYPE_SHARE_GIFT = 25;
	const TYPE_REG_GIFT = 26;

	public static $_type = [
		self::TYPE_SIGN => '签到',
		self::TYPE_WITHDRAW => '提现',
		self::TYPE_PAY_ORDER => '支付订单',
		self::TYPE_TRANSFER => '转帐',
		self::TYPE_REBATE => '订单返利',
//		self::TYPE_CART_REBATE => '购车推荐奖励',
//		self::TYPE_CART_ORDER_OUT => '出局兑换',
		self::TYPE_VOUCHER_EXCHANGE => '兑换淘宝客优惠券',
		self::TYPE_AFTERSALES_RETURN => '售后退款',
		self::TYPE_ORDER_GIVE => '订单赠送',
		self::TYPE_RECHARGE => '充值',
		self::TYPE_PAY_RECHARGE => '支付充值',
		self::TYPE_CARD_RECHARGE => '充值卡充值',
		self::TYPE_WITHDRAW_RETURN => '提现退款',
//		self::TYPE_CARTORDER_GIFT => '购车入单赠送积分',
		self::TYPE_VOUCHER_PDD => '兑换拼多多优惠券',
		self::TYPE_VOUCHER_JD => '兑换京东优惠券',
		self::TYPE_VOUCHER_REBATE => '推广优惠券返利',
		self::TYPE_CALL => '扣除话费',
		self::TYPE_GOODS_DEPOSIT => '商品押金',
		self::TYPE_FUND_DEPOSIT => '退还押金',
		self::TYPE_ORDER_REBACK => '订单退款',
		self::TYPE_ADMIN_OPERATE => '后台操作',
		self::TYPE_ORDER_REIMBURSE => '订单报销',
		self::TYPE_SHARE_GIFT => '分享赠送',
		self::TYPE_REG_GIFT => '注册赠送',
	];
	
	public function getTypeList()
	{
		return self::$_type;
	}
	
	
	
	
	public function getCreateTimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['create_time'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	
	public function getTypeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['type'];
		$list = $this->getTypeList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getOperationTypeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['operation_type'];
		$list = $this->getOperationTypeList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getAccountTypeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['account_type'];
		$list = $this->getAccountTypeList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	protected function setCreateTimeAttr($value)
	{
		return $value && !is_numeric($value) ? strtotime($value) : $value;
	}
	
	public function user(){
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function order(){
		return $this->belongsTo('order', 'order_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	
	/**
	 * 添加流水记录
	 * @param $userId 用户ID
	 * @param $amount 金额
	 * @param float|int $beforeAmount 变动前金额
	 * @param $accountType 帐户类型
	 * @param $operationType 操作类型
	 * @param $type 类型
	 * @param string $orderId 订单ID
	 * @param string $remark 备注
	 * @param int $relation_user_id 相关用户ID
	 * @return bool
	 */
	public function addRecord($userId, $amount, $beforeAmount, $accountType, $operationType, $type, $orderId = '', $remark="", $relation_user_id=''){
		$data = [
			'user_id' => $userId,
			'amount' => $amount,
			'before' => $beforeAmount,
			'account_type' => $accountType,
			'operation_type' => $operationType,
			'type' => $type,
			'order_id' => $orderId,
			'remark' => $remark,
			'create_time' => time(),
			'relation_user_id' => $relation_user_id
		];
		$res = Db::name('fundsRecord')->insert($data);
		if($res){
			$typeList = self::$_type;
			$msg = '';
			$msg .= isset($typeList[$type]) ? $typeList[$type] : '';
			$msg .= $operationType == self::OPE_TYPE_PLUS ? '添加' : '减少';
			$msg .= $amount;
			$accountList = self::$_account_type;
			$msg .= isset($accountList[$accountType]) ? $accountList[$accountType] : '';
			
			//添加信息
			WebMail::addMail($userId, $msg);
			return true;
		}
		return false;
	}
}
